import enquireJs from 'enquire.js'

export function isDef (v){
  return v !== undefined && v !== null
}

/**
 * Remove an item from an array.
 */
export function remove (arr, item) {
  if (arr.length) {
    const index = arr.indexOf(item)
    if (index > -1) {
      return arr.splice(index, 1)
    }
  }
}

const _toString = Object.prototype.toString

export function isRegExp (v) {
  return _toString.call(v) === '[object RegExp]'
}

export function enquireScreen(call) {
  const handler = {
    match: function () {
      call && call(true)
    },
    unmatch: function () {
      call && call(false)
    }
  }
  enquireJs.register('only screen and (max-width: 767.99px)', handler)
}

export function formatLayout(layout,col=4){
  return layout.map((item,index)=>{
    item.x = parseInt(index/col)
    item.y = index%col*2
    item.w = 1
    item.h = 4
    item.i = index
    return item;
  })
}

export function windowMsgPost(data){
  return window.parent.postMessage(data,'*');
}
